package com.jdd.common.util.encryption;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RASUtil {

    /**
     * @param info 需要签名的数据
     * @return
     */
    public static String sign(String info) throws Exception {

        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(BASE64Helper.decryptBASE64(EncryptedString.prikey));
        KeyFactory keyf = KeyFactory.getInstance("RSA");
        PrivateKey myprikey = keyf.generatePrivate(priPKCS8);
        // 用私钥对信息生成数字签名
        java.security.Signature signet = java.security.Signature
                .getInstance("MD5withRSA");
        signet.initSign(myprikey);
        signet.update(info.getBytes("UTF-8"));
        // 对信息的数字签名
        byte[] signed = signet.sign();
        return BASE64Helper.encryptBASE64(signed);

    }

    /**
     * @param sign 签名信息
     * @param info 原数据
     * @return
     */
    public static boolean verifySign(String sign, String info) throws Exception {
        X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(BASE64Helper.decryptBASE64(EncryptedString.pubkey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);
        //这是SignatureData输出的数字签名
//            byte[] signed = hexStrToBytes(sign);
        byte[] signed = BASE64Helper.decryptBASE64(sign);
        java.security.Signature signetcheck = java.security.Signature.getInstance("MD5withRSA");
        signetcheck.initVerify(pubKey);
        signetcheck.update(info.getBytes());
        if (signetcheck.verify(signed)) {
            return true;
        } else {
            return false;
        }


    }


    /**
     * Transform the specified byte into a Hex String form.
     */
    public static final String bytesToHexStr(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);

        for (int i = 0; i < bcd.length; i++) {
            s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
            s.append(bcdLookup[bcd[i] & 0x0f]);
        }

        return s.toString();
    }

    /**
     * Transform the specified Hex String into a byte array.
     */
    public static final byte[] hexStrToBytes(String s) {
        byte[] bytes;

        bytes = new byte[s.length() / 2];

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
                    16);
        }

        return bytes;
    }

    private static final char[] bcdLookup = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static void main(String[] args) throws Exception {
        String sign = sign("钱多多");
        System.err.println(sign);

        Boolean info = verifySign("eUZ5y1VWEQpzzVrLVPopFEmP6/HQtctxQHUC+cafz8qCWKAbB7eUZIxSzphxUedMcJ7VplkcPsvZwuxTKgNltg==",
                "钱多多");
        System.err.println(info);
    }

}

